package com.example.cloud.controller.business.medical;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.basic.Suggest;
import com.example.cloud.model.business.medical.LifeCareAdvice;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.service.basic.info.SuggestService;
import com.example.cloud.service.business.medical.LifeCareAdviceService;
import com.example.cloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangtao on 2018/7/2.
 */
@RestController
@RequestMapping("/life")
public class LifeCareAdviceController extends BaseController {

    @Autowired
    private SuggestService suggestService;

    @Autowired
    private LifeCareAdviceService lifeCareAdviceService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        Result result = this.singleQuery(model, LifeCareAdvice.class);
        JSONArray datas = (JSONArray)result.getData();
        for (Object data : datas) {
            JSONObject json = (JSONObject)data;
            if(json.get("doctorSuggestId") != null){
                String doctorSuggestId = json.get("doctorSuggestId").toString();
                JSONArray idsArr = JSON.parseArray(doctorSuggestId);
                List<Long> ids = new ArrayList<>();
                for (Object id : idsArr) {
                    ids.add(((Integer) id).longValue());
                }
                StringBuilder s = new StringBuilder();
                List<Suggest> list = suggestService.findByIdFromCache(ids);
                for (Suggest suggest : list) {
                    s.append(suggest.getTitle() + " , ");
                }
                json.put("doctorSuggestId", s.toString().substring(0, s.toString().lastIndexOf(" , ")));
            }
        }
        return result;
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id){
        Map<String, Object> params = new HashMap();
        params.put("datas", suggestService.findAllFromCache());
        params.put("data", lifeCareAdviceService.findOne(id));
        return ResultUtil.success(null,params);
    }

    @GetMapping("/findByPatientId/{patientId}")
    public Result findByPatientId(@PathVariable Long patientId){
        return ResultUtil.success(lifeCareAdviceService.findByPatientId(patientId));
    }

    @PostMapping("/saveOrUpdate")
    public void saveOrUpdate(@RequestBody LifeCareAdvice record) {
        lifeCareAdviceService.doSave(record);
    }

    @PostMapping("/addSupplement")
    public void addSupplement(@RequestBody Map map){
        Long id = Long.valueOf(map.get("id").toString());
        String  supplementAdvice = map.get("supplementAdvice").toString();
        lifeCareAdviceService.doAddSupplement(id, supplementAdvice);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody List<Long> ids){
        lifeCareAdviceService.doDelete(ids);
    }
}
